package org.yanfengying.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Entity;
import org.yanfengying.gift.client.LoginUserClient;
import org.yanfengying.gift.domain.Employee;
import org.yanfengying.gift.domain.LoginUser;
import org.yanfengying.gift.domain.Tenant;
import org.yanfengying.gift.dto.TenantDTO;
import org.yanfengying.gift.exception.MyGiftAssert;
import org.yanfengying.gift.mapper.EmployeeMapper;
import org.yanfengying.gift.mapper.TenantMapper;
import org.yanfengying.gift.query.TenantQuery;
import org.yanfengying.gift.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.yanfengying.gift.util.AjaxResult;
import org.yanfengying.gift.util.PageList;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yfy
 * @since 2022-12-06
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LoginUserClient client;
    @Override
    public PageList<Tenant> selectPageList(TenantQuery query) {
        Page<Tenant> page = new Page<Tenant>(query.getPage(),query.getRows());//分页
        List<Tenant> datas =  tenantMapper.loadPageList(page,query);
        return new PageList<>(page.getTotal(), datas);
    }

    /**
     * 店铺入驻
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult settlementShop(TenantDTO dto) {
        //1 参数校验->判空->密码比对
        Employee admin = dto.getAdmin();
        Long mealId = dto.getMealId();
        Tenant tenant = dto.getTenant();
        MyGiftAssert.assertNull(admin,"参数异常请重新输入");
        MyGiftAssert.assertNull(tenant,"参数异常请重新输入");
        /*if(Objects.isNull(dto) || Objects.isNull(admin) || Objects.isNull(tenant) || mealId == null){
            throw new RuntimeException("参数异常请重新输入");
        }*/
        //2 管理员是否存在判断->存在(抛异常)->不存在就新增
        Employee employee = employeeMapper.getByName(admin.getUsername());
        if(Objects.nonNull(employee)){
            throw new RuntimeException("该用户已经存在,请登录后直接入驻!");
        }
        //2.1 不存->保存employee,login_user
        LoginUser loginUser = new LoginUser();
        loginUser.setEmail(admin.getEmail());
        loginUser.setPassword(admin.getPassword());
        loginUser.setTel(admin.getTel());
        loginUser.setType(0);
        loginUser.setUsername(admin.getUsername());
        AjaxResult ajaxResult = client.addOrUpdate(loginUser);
        //3 店铺是否存在->存在(抛异常)->不存在就审核然后新增
        Wrapper<Tenant> wrapper = new EntityWrapper<>();
        List<Tenant> list = tenantMapper.selectList(wrapper.eq("company_name", tenant.getCompanyName()));
        if (list!=null && list.size()>0)
            throw new RuntimeException("该租户已经入住!请直接登录使用");

        if(ajaxResult.isSuccess()){
            String resultObj = ajaxResult.getResultObj().toString();
            Long loginId = Long.valueOf(resultObj);

            Employee employee1 = new Employee();
            employee1.setEmail(admin.getEmail());
            employee1.setLoginId(loginUser.getId());
            employee1.setLoginId(loginId);
            employee1.setTel(admin.getTel());
            employee1.setInputTime(new Date());
            employee1.setPassword(admin.getPassword());
            employeeMapper.insert(employee1);

            //保存租户信息
            tenant.setAdminId(employee1.getId());
            tenant.setRegisterTime(new Date());
            tenantMapper.insert(tenant);
            Map<String, Object> map = new HashMap<>();
            map.put("mealId", mealId);
            map.put("tenantId", tenant.getId());
            map.put("expireDate", DateUtils.addYears(new Date(), 1));
            map.put("state", 0);
            tenantMapper.insertMealTenat(map);
        }


        //4 新增发邮件激活

        return AjaxResult.me();
    }
}
